package cn.jn.lhm.teacher.service.impl;

import cn.jn.lhm.teacher.entity.EduTeacher;
import cn.jn.lhm.teacher.mapper.EduTeacherMapper;
import cn.jn.lhm.teacher.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/5/27 - 21:43
 */
@Service
public class EduTeacherServiceImpl implements EduTeacherService {

    @Resource
    private EduTeacherMapper eduTeacherMapper;

    @Override
    public List<EduTeacher> listByWrap(QueryWrapper<EduTeacher> wrapper) {
        return eduTeacherMapper.selectList(wrapper);
    }

    @Override
    public boolean deleteById(Long id) {
        return eduTeacherMapper.deleteById(String.valueOf(id)) == 1;
    }

    @Override
    public IPage<EduTeacher> listByPage(Page<EduTeacher> page, QueryWrapper<EduTeacher> wrapper) {
        return eduTeacherMapper.selectPage(page, wrapper);
    }

    @Override
    public boolean save(EduTeacher eduTeacher) {
        return eduTeacherMapper.insert(eduTeacher) == 1;
    }

    @Override
    public boolean updateById(EduTeacher eduTeacher) {
        return  eduTeacherMapper.updateById(eduTeacher) == 1;
    }

    @Override
    public EduTeacher getById(Long id) {
        return eduTeacherMapper.selectById(String.valueOf(id));
    }

    @Override
    public Map<String, Object> listByPageToMap(Page<EduTeacher> page) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        eduTeacherMapper.selectPage(page, wrapper);
        Map<String, Object> map = new HashMap<>();
        //分页传入的Page对象和返回的Ipage对象是同一个
        map.put("teacherList", page.getRecords());
        map.put("total", page.getTotal());
        map.put("size", page.getSize());
        map.put("pages", page.getPages());
        map.put("hasNext", page.hasNext());
        map.put("current", page.getCurrent());
        map.put("hasPrevious", page.hasPrevious());
        return map;
    }

    @Override
    public EduTeacher getTeacherInfo(String id) {
        return eduTeacherMapper.selectById(id);
    }


}
